package com.zlsy.thread.concurrentcontainer;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author zhouliang
 * @date 2020/8/31 11:38
 * 对比两个迭代器
 */
public class CopyOnWriteArrayListDemo2 {
    public static void main(String[] args) {
        final CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>(new Integer[]{1, 2, 3});
        System.out.println(list);

        final Iterator<Integer> iterator1 = list.iterator();
        list.add(4);
        System.out.println(list);

        final Iterator<Integer> iterator2 = list.iterator();

        //iterator1能拿到的数据，取决于iterator1获取的时机。在获取iterator1之前还没进行
        //add(4)操作，所以拿不到4
        iterator1.forEachRemaining(System.out::println);
        iterator2.forEachRemaining(System.out::println);
    }
}
